<?php

class Register_model extends CI_Model {

    function __construct() {
        parent::__construct(); {

            $this->load->helper('security');
        }
    }

    function do_register($email, $active_code) {
        //Get data from POST
        $firstname = $this->input->post('firstname');
        $lastname = $this->input->post('lastname');
        $day = $this->input->post('day');
        $month = $this->input->post('month');
        $year = $this->input->post('year');
        $country = $this->input->post('country');
//        $email = $this->input->post('email');
        $password = $this->input->post('password1');
        $gender = $this->input->post('gender');
        $contact = $this->input->post('contact');
        $street1 = $this->input->post('street1');
        $street2 = $this->input->post('street2');
        $postal = $this->input->post('postal');
        $unitnumber = $this->input->post('unitNumber');
        $dob = $year . '-' . $month . '-' . $day;
        $sha1_pwd = SHA1($password);
        $query = "INSERT INTO student(first_name,last_name,password,email,gender,contact,date_of_birth,street_name_1,street_name_2,unit_num,postal_code,active_code,country_origin_id)
                  VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)";
        $this->db->query($query, array($firstname, $lastname, $sha1_pwd, $email, $gender, $contact, $dob, $street1, $street2, $unitnumber, $postal, $active_code, $country));
    }

    function check_email_exists($email) {
        $query = "SELECT * FROM student WHERE email = ?";
        $result = $this->db->query($query, $email);

        if ($result->num_rows() > 0) {
            //email exists
            return true;
        } else {
            //email does not exist
            return false;
        }
    }

    function check_captcha_exists() {
        //delete existing captcha > 2 hours
        $expiration = time() - 7200;
        $sql = "DELETE FROM captcha WHERE captcha_time < ? ";
        $binds = array($expiration);
        $query = $this->db->query($sql, $binds);
        // Then see if a captcha exists:
        $exp = time() - 600;
        $sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?";
        $binds = array($this->input->post('captcha'), $this->input->ip_address(), $exp);
        $query = $this->db->query($sql, $binds);
        $row = $query->row();
        if ($row->count == 0) {
            //captcha exists
            return false;
        } else {
            //captcha does not exist
            return true;
        }
    }

    function confirm_registration($active_code) {
        $query = "SELECT student_id FROM student WHERE active_code = ?";
        $result = $this->db->query($query, $active_code);

        if ($result->num_rows() > 0) {
            $query = "UPDATE student SET activate = 1 WHERE active_code = ?";
            $this->db->query($query, $active_code);

            return true;
        } else {
            return false;
        }
    }

    function country_list() {
        $query = $this->db->order_by("country_name", "asc");
        $query = $this->db->get('country');
        $data = array(
            'countrynull' => 'Select Country',
        );
        foreach ($query->result_array() as $row) {
            $data[$row['country_id']] = $row['country_name'];
        }
        return $data;
    }

    function change_pw() {
        //change to session after creating
        $user_id = 1;
        $password = $this->input->post('new_password');
        $pw = SHA1($password);
        $data = array(
            'password' => $pw
        );
        $where = "student_id = $user_id";
        $query = $this->db->update('student', $data, $where);
        return $query;
    }

    function forgot_pw($email, $random_pw) {
        $query = "SELECT * FROM student WHERE email ='$email'";
        $result = $this->db->query($query);
//        $query = "SELECT * FROM student WHERE email = ? AND student_id = ?";
//        $result = $this->db->query($query, $email, $student_id);
        if ($result->num_rows() > 0) {
            $pw = SHA1($random_pw);
            $data = array(
                'password' => $pw
            );
            $where = "email = '$email'";
            $query = $this->db->update('student', $data, $where);
            return $query;
        } else {
            return false;
        }
    }

    //upadte profile
    function do_update_profile() {
        //change to session after creating
        $user_id = 1;
        $day = $this->input->post('day');
        $month = $this->input->post('month');
        $year = $this->input->post('year');
        $dob = $year . '-' . $month . '-' . $day;
        $data = array(
            'first_name' => $this->input->post('firstname'),
            'last_name' => $this->input->post('lastname'),
            'email' => $this->input->post('email'),
            'country_origin_id' => $this->input->post('country'),
            'gender' => $this->input->post('gender'),
            'contact' => $this->input->post('contact'),
            'date_of_birth' => $dob
        );
        $where = "student_id =" . $user_id;
        $query = $this->db->update('student', $data, $where);
        return $query;
    }

}

?>
